#include "aodopencv/jf_aod_opencv.h"
#include "unistd.h"
int setpos = 0;
int g_Pos = 0;

string readFile_dir = "/home/jetflow/lidongwei/taigu/";
string readFile_name = "test2.mp4"; 
string readFile = readFile_dir+readFile_name;
CvCapture* g_Capture;
VideoCapture cap1(readFile);


void CallBackTrackBarSlide(int pos)  
{  
    // 这里便是重新设定视频文件当前播放的帧  
    cvSetCaptureProperty(g_Capture,CV_CAP_PROP_POS_FRAMES,pos);
	cap1.set(CV_CAP_PROP_POS_FRAMES, pos);  
    g_Pos = pos;  
}

Point getCenterPoint(Rect rect);

int main(int argc, char* argv[]){
    /*
        video directory:
        1./home/jetflow/lidongwei/taigu/sample*.mp4; * represents a 1-2 number
        2./home/jetflow/lidongwei/pic/aban3.mp4
        3./home/jetflow/wudi/TestVideos/taigu/events/throw-rubbish-out-window.mp4
        4./home/jetflow/wudi/TestVideos/guojian/video/*.avi
    */
    //VideoCapture cap1("/home/jetflow/wudi/TestVideos/taigu/events/throw-rubbish-out-window.mp4");
     
     auto x = [&](AODOutput ao){
        // cout<<"execute callback function"<<endl;
        cv::Mat mat  = ao.mat;
        vector<AODResult> arrRes = ao.arrRes;
        for(int i = 0; i < arrRes.size(); i++){
            rectangle(mat,cv::Point(arrRes[i].rt.x, arrRes[i].rt.y),cv::Point(arrRes[i].rt.x+arrRes[i].rt.width, arrRes[i].rt.y+arrRes[i].rt.height), cvScalar(201, 102, 62), 3, 4, 0);
        }
        imshow("frame",mat);
        string filename = "/data/ldw/record_img/"+readFile_name+"_"+to_string(g_Pos)+".jpg";
        imwrite(filename, mat);
        //  rectangle(frame,cv::Point(face_info[0].x0, face_info[0].y0),cv::Point(face_info[0].x1, face_info[0].y1), cvScalar(201, 102, 62), 3, 4, 0);
    };
    
    g_Capture = cvCreateFileCapture(readFile.c_str());
    g_Pos = setpos; 
    //namedWindow("frame");
    //namedWindow("sub");
    //namedWindow("detection");
    cap1.set(CV_CAP_PROP_POS_FRAMES, setpos);
    int totalFrames =  (int)cvGetCaptureProperty(g_Capture, CV_CAP_PROP_FRAME_COUNT);
    if(totalFrames != 0)  
    {  
        //创建滚动条，在这里指定了滚动条拖动后的回调函数  
        cvCreateTrackbar("TrackBar","frame",  
            &g_Pos,totalFrames,CallBackTrackBarSlide);  
    }
    AODOpenCV aod;
    aod.setCallBack(x);
    cv::Rect rt = cv::Rect(200,200,600,500);
    aod.setROI(rt);
    aod.setAreaThresholdMin(20);
    aod.setAreaThresholdMax(20000);
    //aod.setaccurate(200);
    aod.setUseFrequencyTactics(true);
    aod.setUseAreaComparison(true);
    aod.setUseAlarmTimeTactic(true);
    //aod.setRatioLevel(18); // 16 -> 0.8, 17 -> 0.85, 18 -> 0.9
    aod.setinterval(120);
    aod.setaccurate(20);
    aod.setthrescount(100);
    aod.setTimeGapThreshold(3000);
    aod.setmaxDiscontinuedTimes(2);
   // cvMoveWindow("frame", 50, 50);
    //cvMoveWindow("sub", 850, 50);
    cv::Mat origin_mat;
   // cv:Rect rt(200,200,600,500);
    double t; 
    double total = 0;
    int count = 0;
    
    // int x = 100;
    // int y = 50;
    // int w = 280;
    // int h = 350;
    // cv::Rect rect(x, y, w, h);

    for(;;)
	{
		//t = getTickCount() / getTickFrequency();
        cap1 >> origin_mat;
       
        // origin_mat(rect).copyTo(origin_mat);
        //cout<<origin_mat.cols<<"x"<<origin_mat.rows<<endl;
        //cout<<origin_mat.cols<<"x"<<origin_mat.rows<<endl;
		if (origin_mat.empty()) break;
        count++;
        t = getTickCount() / getTickFrequency();
       // resize(origin_mat,origin_mat,Size(320,260));
        AODInput aodinput;
        aodinput.mat = origin_mat;
        aod.detect(aodinput);
        total +=  (getTickCount() / getTickFrequency() -t);
        vector<Rect> result = aod.getdetectedRect();
        if(!result.empty()){
            cout<<"detect abandon object successful"<<endl;
            for(int i = 0; i < result.size(); i++){
                circle(origin_mat(rt), getCenterPoint(result[i]), 4, Scalar(0, 255, 255), 2, 8);
               // cout<<"detect abadon is "<<aod.getCenterPoint(result[i])<<endl;
            }          
        }
        //aod.cleardetectedRect();
    //    imshow("test", origin_mat(rt));
        //imshow("sub", aod.getsub());
      // imshow("sub",aod.getsub());
        double average = total/count;
        //if(count%300 == 0) cout<<"average processing time of one frame is "<<average<<endl; 
        // if (cv::waitKey(25) > 0) waitKey(0);
        usleep(25000);
        g_Pos++;

        if(g_Pos % 500 == 0) cout<<"the current frame is "<<g_Pos<<endl;
       // cvSetTrackbarPos("TrackBar","frame",g_Pos); 

    }
    cout<<"video play finish"<<endl;
    return 0;
}

//获取rect中心点
Point getCenterPoint(Rect rect)
{
	Point cpt;
	cpt.x = rect.x + cvRound(rect.width / 2.0);
	cpt.y = rect.y + cvRound(rect.height / 2.0);
	return cpt;
}